Marshal managed string[] to unmanaged char**

Posted by Vince on Stack Overflow See other posts from Stack Overflow or by Vince
Published on 2010-04-20T02:04:55Z Indexed on 2010/04/20 2:13 UTC
Read the original article Hit count: 293

Filed under:
|
|
|

This is my c++ struct (Use Multi-Byte Character Set)

typedef struct hookCONFIG {
    int threadId;
    HWND destination;

    const char** gameApps;
    const char** profilePaths;
} HOOKCONFIG;

And .Net struct

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct HOOKCONFIG {
    public int threadId;
    public IntPtr destination;

    // MarshalAs?
    public string[] gameApps;

    // MarshalAs?
    public string[] profilePaths;
}

I got some problem that how do I marshal the string array? When I access the struct variable "profilePaths" in C++ I got an error like this:

An unhandled exception of type 'System.AccessViolationException' occurred in App.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

MessageBox(0, cfg.profilePaths[0], "Title", MB_OK); // error ... Orz

© Stack Overflow or respective owner

Related posts about c#

Related posts about c++